package com.example.chcrm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chcrm.entity.common.OperateRecord;
import com.example.chcrm.entity.common.RandomHelper;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.consignor.CustomerCarBrand;
import com.example.chcrm.entity.employee.Department;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.mapper.CustomerCarBrandMapper;
import com.example.chcrm.mapper.OperateRecordMapper;
import com.example.chcrm.service.CustomerCarBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 邹
 * @Date 2025/4/20 17:48
 * @Version 1.0
 */
@Service
public class CustomerCarBrandServiceImpl<T> implements CustomerCarBrandService<T> {
    
    @Autowired
    private CustomerCarBrandMapper customerCarBrandMapper;
    @Autowired
    private OperateRecordMapper operateRecordMapper;
    @Override
    public ResponseMsg<IPage<CustomerCarBrand>> pageList(CustomerCarBrand customerCarBrand, EmployeeVo empVo) {
        QueryWrapper<CustomerCarBrand> query = new QueryWrapper<>();
        
        // 品牌名称查询条件
        if (StringUtils.isNotBlank(customerCarBrand.getBrand())) {
            query.like("brand", customerCarBrand.getBrand());
        }
        
        // 状态查询条件
        if (customerCarBrand.getStatus() != null) {
            query.eq("status", customerCarBrand.getStatus());
        }
        
        // 只查询未删除的记录
        query.eq("is_deleted", 0);
        
        // 按创建时间倒序排序
        query.orderByDesc("create_time");
        
        // 执行查询
        List<CustomerCarBrand> list = customerCarBrandMapper.selectList(query);
        
        // 封装返回结果
        ResponseMsg<IPage<CustomerCarBrand>> responseMsg = new ResponseMsg<>();
        Page<CustomerCarBrand> page = new Page<>();
        page.setRecords(list);
        page.setTotal(list.size());
        page.setSize(customerCarBrand.getRows());
        page.setCurrent(customerCarBrand.getPage());
        
        responseMsg.setData(page);
        responseMsg.success("查询成功");
        return responseMsg;
    }

    @Override
    public ResponseMsg<Object> detail(String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        CustomerCarBrand brand = customerCarBrandMapper.selectById(id);
        if (brand == null) {
            msg.fail("品牌不存在");
            return msg;
        }
        msg.setData(brand);
        msg.success("查询成功");
        return msg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> update(CustomerCarBrand customerCarBrand, EmployeeVo empVo) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (customerCarBrand.getId() == null) {
            msg.fail("品牌ID不能为空");
            return msg;
        }
        CustomerCarBrand existBrand = customerCarBrandMapper.selectById(customerCarBrand.getId());
        if (existBrand == null) {
            msg.fail("品牌不存在");
            return msg;
        }
        customerCarBrandMapper.updateById(customerCarBrand);
        //添加操作日志
        OperateRecord ope = new OperateRecord();
        ope.setId(RandomHelper.uuid());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ope.setCreateTime(sdf.format(new Date()));
        ope.setOperateType("修改");
        if (empVo != null) {
            ope.setOperationId(empVo.getId());
            ope.setOperationName(empVo.getEmpNm());
        }
        ope.setSourceId(customerCarBrand.getId());
        operateRecordMapper.insert(ope);
        msg.success("修改成功");
        return msg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> disable(CustomerCarBrand customerCarBrand, EmployeeVo empVo) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (customerCarBrand.getId() == null) {
            msg.fail("品牌ID不能为空");
            return msg;
        }
        CustomerCarBrand existBrand = customerCarBrandMapper.selectById(customerCarBrand.getId());
        if (existBrand == null) {
            msg.fail("品牌不存在");
            return msg;
        }
        existBrand.setStatus(customerCarBrand.getStatus());
        customerCarBrandMapper.updateById(existBrand);
        //添加操作日志
        OperateRecord ope = new OperateRecord();
        ope.setId(RandomHelper.uuid());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ope.setCreateTime(sdf.format(new Date()));
        ope.setOperateType("修改状态");
        if (empVo != null) {
            ope.setOperationId(empVo.getId());
            ope.setOperationName(empVo.getEmpNm());
        }
        ope.setSourceId(customerCarBrand.getId());
        operateRecordMapper.insert(ope);
        msg.success("修改状态成功");
        return msg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> add(CustomerCarBrand dept,EmployeeVo empVo) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (StringUtils.isBlank(dept.getBrand())){
            msg.fail("请填写部门名称");
            return msg;
        }
        //验证部门名称是否存在
        int num = (customerCarBrandMapper.selectCount(new QueryWrapper<CustomerCarBrand>()
                .eq("brand",dept.getBrand())
                .eq("is_deleted",0))).intValue();
        if(num > 0){
            msg.fail("品牌已存在");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        String id = RandomHelper.uuid();
        dept.setId(id);
        dept.setStatus(1);
        dept.setIsDeleted(0);
        dept.setCreateTime(new Date());
        customerCarBrandMapper.insert(dept);
        //添加操作日志
        OperateRecord ope = new OperateRecord();
        ope.setId(RandomHelper.uuid());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ope.setCreateTime(sdf.format(new Date()));
        ope.setOperateType("添加");
        if (empVo != null) {
            ope.setOperationId(empVo.getId());
            ope.setOperationName(empVo.getEmpNm());
        }
        ope.setSourceId(id);
        operateRecordMapper.insert(ope);
        msg.success("新增成功");
        return msg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> selectAllBrand() {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        QueryWrapper<CustomerCarBrand> query = new QueryWrapper<>();
        query.select("id,brand");
        query.eq("is_deleted", 0).eq("status",1);
        List<Map<String, Object>> essRole = customerCarBrandMapper.selectMaps(query);
        msg.setData(essRole);
        msg.success("查询成功");
        return msg;
    }
}
